/**
 * 
 */
package com.laozhao.EasySales.util;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingWorker;

/**
 * @author laozhao
 *
 */
public class StatusRow extends JPanel implements ActionListener{

	private JWindow popWindow;
	private JPanel list;
	private HashMap<String, Thread> threadMap;
	private HashMap<String, ProgressPanel> ProgressMap;
	private String selectKey;
	/**
	 * 
	 */
	public StatusRow() {
		super();
		initComponents();
	}

	
	private void initComponents() {
		list = new JPanel();
		list.setLayout(new GridLayout(2147483647,1,5,5));
		
		selectKey = "";
		
		threadMap.put(selectKey, null);
		ProgressMap.put(selectKey, new ProgressPanel(selectKey,""));
		
	}
	
	private void buildProgressPanel(){
		
	}
	
	//event
	public void setProgress(int Progress){
		setProgress(selectKey,"",Progress);
	}
	public void setProgress(String key , int Progress){
		setProgress(key,"",Progress);
	}
	public void setProgress(String key , String name, int Progress){
		ProgressPanel setProgress = ProgressMap.get(key);
		setProgress.getNameLabel().setText(name);
		setProgress.getProgressBar().setValue(Progress);
	}

	//创建一个进度条
	public void creatProgress(String key,Thread th){
		creatProgress(key,key,th);
	}
	
	public void creatProgress(String key,String name,Thread th){
		//如果key没有重复，则添加一个新的ProgressPanel到list
		if(ProgressMap.get(key) == null){
			//添加一个新的ProgressPanel到list
			addProgress(key,name , th);
		}
	}
//	//移除一个进度条
//	public void removeProgress(){
//		
//	}

	private void addProgress(String key,String name,Thread th) {
//		final String ProgressName = name;
		if(list.getComponentCount() <= 2147483647){
			ProgressPanel Progress = new ProgressPanel(key,name);
			list.add(Progress);
			
			threadMap.put(key, th);
			ProgressMap.put(key, Progress);
			
		}
	}


	public void actionPerformed(ActionEvent e) {
		if(e.getSource() instanceof JButton){
			String key = e.getActionCommand();
			if(threadMap.get(key) != null){
				ProgressPanel Progress = ProgressMap.get(key);
				list.remove(Progress);
				
				Thread th = threadMap.get(key);
//				th.stop();
//				th.destroy();
				th.interrupt();
				th = null;
				System.gc();
			}
		}
	}

//	public static void main(String[] s){
//		sleep sp = new sleep();
//		sp.start();
//	}
//	public class sleep extends Thread{
//		public sleep(){
//			int i = 0 ;
//			while(i < 30){
//				try {
//					Thread.sleep(1000);
//					i++;
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//			
//		}
//	}
	private class ProgressPanel extends JPanel{
		
		/**
		 * 
		 */
		private static final long serialVersionUID = 5269663688362947761L;
		private JLabel nameLabel = new JLabel();
		private JButton controlButton = new JButton();
		private JProgressBar ProgressBar = new JProgressBar();
		private JPanel ProgressPanel = new JPanel(new GridLayout(2,2,5,5));
		
//		private ProgressPanel(){
//			super();
//			ProgressPanel("");
//		}
		private ProgressPanel(String key ,String name){
			int hight = ProgressBar.getHeight();
			int width = ProgressBar.getWidth();
			
			nameLabel.setSize(width - hight, hight);
			nameLabel.setText(name);
			
			controlButton.setText("X");
			controlButton.setActionCommand(key);
			controlButton.setForeground(Color.red);
			controlButton.setSize(hight, hight);
			
			ProgressBar.setMinimum(0);
			ProgressBar.setMinimum(100);
			ProgressPanel.add(nameLabel);
			ProgressPanel.add(controlButton);
			ProgressPanel.add(ProgressBar);
			
			
			new SwingWorker<Void, Void>(){
				@Override
				protected Void doInBackground() throws Exception {
					setActive();
					return null;
				}
				@Override  
		        protected void done() {   
		            
				}
			};
//			threadMap.get(key);
		}

		public JLabel getNameLabel() {
			return nameLabel;
		}

		public JButton getControlButton() {
			return controlButton;
		}

		public JProgressBar getProgressBar() {
			return ProgressBar;
		}
		
		public synchronized void setActive(){
			boolean x = true;
			while((threadMap.get(controlButton.getActionCommand())).isAlive()){
				if(x)
					controlButton.setText("+");
				else
					controlButton.setText("X");
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

}
